package com.government.common.utils;


import com.government.common.core.domain.BaseTreeVo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class TreeUtils {

    /**
     * 节点递归依次搜寻  根节点->父节点->子节点
     *
     * @param parentNode 顶级节点
     * @param allNodeMap 所有节点Map
     * @param <T>        参数类型集继承自{@link  BaseTreeVo}
     * @return List<T> 返回一个树的集合
     */
    public static <T extends BaseTreeVo> List<T> buildTree(List<T> parentNode, HashMap<Long, List<T>> allNodeMap) {
        ArrayList<T> currentNodes = new ArrayList<>();
        for (T node : parentNode) {
            Long id = Long.parseLong(node.getId());
            List<T> childList = allNodeMap.get(id);
            if (null != childList) {
                childList = buildTree(childList, allNodeMap);
                node.setChildren(childList);
            }

            currentNodes.add(node);
        }
        return currentNodes;
    }

    public static <T extends BaseTreeVo> List<T> buildTree1(List<T> parentNode, HashMap<Long, List<T>> allNodeMap) {
        ArrayList<T> currentNodes = new ArrayList<>();
        for (T node : parentNode) {
            Long id = Long.parseLong(node.getId());
            List<T> childList = allNodeMap.get(id);
            if (null != childList) {
                childList = buildTree(childList, allNodeMap);
                node.setChildren(childList);
            }

            currentNodes.add(node);
        }
        return currentNodes;
    }

}
